<?php

class Form_Pages extends Cms_FormProcessor
{
    protected $db = null;               // объект для соединения с базой данных
    public $pages = null;                // объект класса DatabaseObject_User
    protected $_validateOnly = false;   // флаг, указывающий тип запроса: true = ajax
    
    public function __construct($db, $id = 0) {
        parent::__construct();
        
        $this->db = $db;
        $this->pages = new Model_Pages($this->db);
        if ($id!='0'){
        $this->pages->load($id);
        $this->id = $this->pages->getId();
        $this->name = $this->pages->name;
        $this->h1 = $this->pages->h1;
        $this->description = $this->pages->description;
        $this->keywords = $this->pages->keywords;
        $this->alias = $this->pages->alias;
        $this->short = $this->pages->short;
        $this->full = $this->pages->full;
      
        }
        
    }
    
    public function process(Zend_Controller_Request_Abstract $request) {
        // проверим название
        $this->name = trim($request->getPost('name'));

        if (strlen($this->name) == 0)
            $this->addError('name', 'Вы должны указать название');
        else
            $this->pages->name = $this->name;
        
        $this->h1 = trim($request->getPost('h1'));

        if (strlen($this->h1) == 0)
            $this->addError('name', 'Вы должны указать название');
        else
            $this->pages->h1 = $this->h1;
        
        $this->description = trim($request->getPost('description'));

        if (strlen($this->description) == 0)
            $this->addError('name', 'Вы должны указать название');
        else
            $this->pages->description = $this->description;
        
        $this->keywords = trim($request->getPost('keywords'));

        if (strlen($this->keywords) == 0)
            $this->addError('keywords', 'Вы должны указать название');
        else
            $this->pages->keywords = $this->keywords;
        
        $this->alias = trim($request->getPost('alias'));

        if (strlen($this->alias) == 0)
            $this->addError('alias', 'Вы должны указать название');
        else
            $this->pages->alias = $this->alias;
        
        $this->short = trim($request->getPost('short'));

        if (strlen($this->short) == 0)
            $this->addError('short', 'Вы должны указать название');
        else
            $this->pages->short = $this->short;
        
        $this->full = trim($request->getPost('full'));

        if (strlen($this->full) == 0)
            $this->addError('full', 'Вы должны указать название');
        else
            $this->pages->full = $this->full;
        
            $this->pages->date =  date("Y.m.d h:i:s");
        
        
        // если не было ошибок, сохраним пользователя
        if (!$this->_validateOnly && !$this->hasError()) {
            $this->pages->save();
            unset($session->phrase);
        }
        
        return !$this->hasError();
    }
}

  /*  public function init()
    {
        $this->setName('news');
        $id = new Zend_Form_Element_Hidden('id');
        $id->addFilter('Int');
        
        $isEmptyMessage = 'Значение является обязательным и не может быть пустым';
        
        $name = new Zend_Form_Element_Text('name');
        $name->setLabel('Название')
            ->setRequired(true)
            ->addFilter('StripTags')
            ->addFilter('StringTrim')
            ->addValidator('NotEmpty', true,
                array('messages' => array('isEmpty' => $isEmptyMessage))
            );
        
        $h1 = new Zend_Form_Element_Text('h1');
        $h1->setLabel('Тег H1')
            ->setRequired(true)
            ->addFilter('StripTags')
            ->addFilter('StringTrim')
            ->addValidator('NotEmpty', true,
                array('messages' => array('isEmpty' => $isEmptyMessage))
            );
        
        $description = new Zend_Form_Element_Text('description');
        $description->setLabel('Тег description')
            ->setRequired(true)
            ->addFilter('StripTags')
            ->addFilter('StringTrim')
            ->addValidator('NotEmpty', true,
                array('messages' => array('isEmpty' => $isEmptyMessage))
            );
        
        $keywords = new Zend_Form_Element_Text('keywords');
        $keywords->setLabel('Тег keywords1')
            ->setRequired(true)
            ->addFilter('StripTags')
            ->addFilter('StringTrim')
            ->addValidator('NotEmpty', true,
                array('messages' => array('isEmpty' => $isEmptyMessage))
            );
        
        $alias = new Zend_Form_Element_Text('alias');
        $alias->setLabel('SEO URL')
            ->setRequired(true)
            ->addFilter('StripTags')
            ->addFilter('StringTrim')
            ->addValidator('NotEmpty', true,
                array('messages' => array('isEmpty' => $isEmptyMessage))
            );
        
        $short = new Zend_Form_Element_Textarea('short');
        $short->setLabel('Краткий текст')
            ->setRequired(true)
            ->addFilter('StripTags')
            ->addFilter('StringTrim')
            ->addValidator('NotEmpty', true,
                array('messages' => array('isEmpty' => $isEmptyMessage))
            );
        
        $full = new Zend_Form_Element_Textarea('full');
        $full->setLabel('Полный текст')
            ->setRequired(true)
            ->addFilter('StringTrim')
            ->addValidator('NotEmpty', true,
                array('messages' => array('isEmpty' => $isEmptyMessage))
            );
          
        
        $submit = new Zend_Form_Element_Submit('submit');
        $submit->setAttrib('id', 'submitbutton');
        $this->addElements(array($id,$name,$h1,$description,$keywords,$alias,$short,$full, $submit));
    }
}*/